package com.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** * HttpConnection GET/SET 请求 * @author tdw * */
public class HttpConnection {
	private static final Logger logger = LoggerFactory.getLogger(HttpConnection.class);
	private static int connectTimeout = 2000;
	private static int readTimeout = 2000;

	public static int getConnectTimeout() {
		return connectTimeout;
	}

	/** * 设置请求超时时间，默认为2000ms * @param connectTimeout */
	public static void setConnectTimeout(int connectTimeout) {
		HttpConnection.connectTimeout = connectTimeout;
	}

	public static int getReadTimeout() {
		return readTimeout;
	}

	/** * 设置请求处理超时时间，默认为2000ms * @param readTimeout */
	public static void setReadTimeout(int readTimeout) {
		HttpConnection.readTimeout = readTimeout;
	}

	/** * * @param requestUrl 请求URL地址 * @param encoding 结果编码处理 * @return String */
	public static String doPostRequest(String requestUrl, String encoding) {
		HttpURLConnection urlConnection = null;
		StringBuffer buffer = new StringBuffer();
		try {
			URL url = new URL(requestUrl);
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("POST");
			urlConnection.setConnectTimeout(getConnectTimeout());
			urlConnection.setReadTimeout(getReadTimeout());
			String ip = StringUtil.randIP();
			urlConnection.setRequestProperty("X-Forwarded-For",ip);
			 urlConnection.setRequestProperty("HTTP_X_FORWARDED_FOR",ip);
			 urlConnection.setRequestProperty("HTTP_CLIENT_IP",ip);
			 urlConnection.setRequestProperty("REMOTE_ADDR",ip);
			
			urlConnection.connect();
			InputStream in = urlConnection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in,
					encoding));
			String line = null;
			while ((line = rd.readLine()) != null) {
				buffer.append(line);
			}
			rd.close();
			in.close();
		} catch (Exception e) {
			// System.out.println(reqUrl+"请求失败:",e);
			e.printStackTrace();
		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
		return buffer.toString();
	}

	/**
	 * * 像目标地址发送GET请求 * @param requestUrl 请求URL地址 * @param encoding 结果编码处理 * @return
	 * String
	 */
	public static String doGetRequest(String requestUrl, String encoding) {
		StringBuffer buffer = new StringBuffer();
		HttpURLConnection urlConnection = null;
		try {
			logger.info("###http send url="+requestUrl);
			URL url = new URL(requestUrl);
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.setConnectTimeout(getConnectTimeout());
			urlConnection.setReadTimeout(getReadTimeout());
			urlConnection.connect();
			BufferedReader read = new BufferedReader(new InputStreamReader(
					urlConnection.getInputStream(), encoding));
			String str = "";
			while ((str = read.readLine()) != null) {
				buffer.append(str);
			}
			logger.info("###http response = "+buffer.toString());
			read.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		String url="http://gd.10086.cn/epay-portal/charge/charge_input.action?c_id=01&cd_type=0&merchant=01040755001&srv_id=0&p_method=ChinaPayExpress%2CChinaPayUnion%2CCMPayUnion%2CCMPayAccount%2CCMPayExpress&o_id=001075500100251201501061115035670378&o_time=20150106113749&user=13715192521&point=0&amount=9900&title=%E5%B9%BF%E4%B8%9C%E7%A7%BB%E5%8A%A8%E5%AE%98%E7%BD%91%E5%85%85%E5%80%BC&back_url=http%3A%2F%2Fgd.10086.cn%2Fepay%2Funifront&notify_url=http%3A%2F%2Fgd.10086.cn%2Fepay%2Funiback&digest=KqizAd6MF7RrkIzyG3HKkw%3D%3D&desc=%E5%B9%BF%E4%B8%9C%E7%A7%BB%E5%8A%A8%E5%AE%98%E7%BD%91%E5%85%85%E5%80%BC&p_direct=0";
		String msg = doGetRequest(url,"UTF-8");
		System.out.println("####msg: "+msg);
	}
	
//	public static void batchRequest(){
//		SimpleDateFormat sdf 
//		while(true){
//			
//			
//			
//			
//			
//			try {
//				Thread.sleep(500);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}
//		
//		
//	}
}
